package com.ourteam.product.requirement.api;

import java.util.Map;

import com.ourteam.frame.web.AbstractTreeNodeBuilder;
import com.ourteam.product.requirement.dao.IProductVersionDAO;
import com.ourteam.product.requirement.dao.ProductVersionDAOQueryBean;
import com.ourteam.product.requirement.domain.ProductDefineBean;
import com.ourteam.product.requirement.domain.ProductVersionBean;
import com.ourteam.product.requirement.service.IProductService;
import com.ourteam.product.requirement.service.IProductVersionService;
import com.ourteam.product.requirement.service.ProductServiceFactory;
import com.ourteam.product.requirement.service.ProductVersionServiceFactory;

import net.ui.UIConfigFactory;

public class ProductVersionSubNodeBuilder extends AbstractTreeNodeBuilder<ProductDefineBean, ProductVersionBean> {

	private static final IProductService PRODUCT_SERVICE = ProductServiceFactory.getProductService();

	private static final IProductVersionService PRODUCT_VERSION_SERVICE = ProductVersionServiceFactory
			.getProductVersionService();

	@Override
	public ProductVersionBean[] getTreeNodeObjects(ProductDefineBean parentObject) throws Exception {

		ProductVersionDAOQueryBean queryBean = new ProductVersionDAOQueryBean();

		queryBean.createCriteria().andProductDefineIdEqualTo(parentObject.getId());

		queryBean.setSortProperty(IProductVersionDAO.StartDate);

		return PRODUCT_VERSION_SERVICE.queryProductVersions(queryBean);
	}

	@Override
	public Object getNodeId(ProductVersionBean childObject) throws Exception {
		return childObject.getId();
	}

	@Override
	public boolean isLeaf(ProductVersionBean childObject) throws Exception {
		return true;
	}

	@Override
	public int getChildCount(ProductVersionBean childObject) throws Exception {

		ProductVersionDAOQueryBean queryBean = new ProductVersionDAOQueryBean();

		queryBean.createCriteria().andParentVersionIdEqualTo(childObject.getId());

		return PRODUCT_VERSION_SERVICE.queryProductVersionCount(queryBean);
	}

	@Override
	public String getNodeDefineId(ProductVersionBean childObject) throws Exception {
		return "productVersion";
	}

	@Override
	public ProductDefineBean converToParentObject(Object parent) throws Exception {
		return PRODUCT_SERVICE.getProductDefineById(Long.parseLong(parent.toString()));
	}

	@Override
	public String getTag(ProductVersionBean childObject) throws Exception {
		Map<Object, String> versionStatusList = UIConfigFactory.getInstance("product.productVersionStatus")
				.getListDataConfigMap("product.productVersionStatus");
		return versionStatusList.get(childObject.getStatus());
	}

}
